// pages/users/index.js
const {
    wxLogin,
    wxMobileLogin,
    decodeUserInfo,
    updateUserInfo,
    sysSetting
} = require('../../../api/login')
Page({

    /**
     * 页面的初始数据
     */
    data: {
        sexOptions: ['男', '女'],
        cityOptions: [{
                "id": "110000",
                "name": "北京",
                "cityList": [{
                    "id": "110000",
                    "name": "北京",
                    "cityList": [{
                        "id": "110101",
                        "name": "东城区"
                    }]
                }]
            },
            {
                "id": "110001",
                "name": "湖南",
                "cityList": [{
                    "id": "110000",
                    "name": "长沙",
                    "cityList": [{
                        "id": "110101",
                        "name": "天心区"
                    }]
                }]
            }
        ],
        region: ['广东省', '广州市', '海珠区'],
        sex: 0,
        nickname: '',
        mobile: '',
        userInfo: {}
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        this.setData({
          userInfo:wx.getStorageSync('userInfo')
        })
    },
    async fetchUserInfo() {
        const {
            result
        } = await decodeUserInfo()
        const initCity=wx.getStorageSync('initCity').split(',')
        this.setData({
            nickname: result.nickname,
            mobile:result.mobile,
            sex:result.sex,
            region:result.city?result.city.split(',')||initCity:initCity
        })
    },
    async getPhoneNumber(e) {
        wx.showLoading({
            title: '授权中',
        })
        const {
            encryptedData,
            iv
        } = e.detail

        wx.login({
            success: async function (res) {
                const param = {
                    code: res.code,
                    encryptedData,
                    iv
                }
                const data = await wxMobileLogin(param)
                wx.hideLoading()
                if (data.success) {
                    wx.showToast({
                        title: '授权成功',
                    })
                    this.fetchUserInfo()
                }
            }
        })


    },
    inputChange(e) {
        const type = e.currentTarget.dataset.type
        this.setData({
            [type]: e.detail.value
        })
    },
    bindPickerChange: function (e) {
        this.setData({
            sex: e.detail.value
        })
    },
    bindRegionChange: function (e) {
        this.setData({
            region: e.detail.value
        })
    },

    async save() {
        const {
            mobile,
            nickname,
            region,
            sex
        } = this.data
        const params = {
            mobile,
            nickname,
            city:region.join(','),
            sex:parseInt(sex)
        }
        const {result}=await updateUserInfo(params)
        if(result){
            wx.showToast({
              title: '更新成功',
              icon:'none'
            })
        }
    }
})